package com.example.demo.project.sys;



import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 系统共用
 */
public final class SysCommon {


	
	public static final class SafeDateFormat {
		private ThreadLocal<DateFormat> threadLocal;
		private SafeDateFormat(final String format) {
			threadLocal = new ThreadLocal<DateFormat>() {
		        @Override
		        protected synchronized DateFormat initialValue() {
		            return new SimpleDateFormat(format);
		        }
		    };
		}
	    public Date parse(String dateStr) {
	    	if (null == dateStr) return null;
	    	dateStr = dateStr.trim();
	    	if (0 == dateStr.length()) return null;
			try {
				return threadLocal.get().parse(dateStr);
			} catch (ParseException e) {
				throw new IllegalStateException(e);
			}
		}

	    public String format(Object date) {
	    	if (null == date) return "";
	        return threadLocal.get().format(date);
	    }
	}
	
	public final static SafeDateFormat YMD = new SafeDateFormat("yyyy-MM-dd");
	public final static SafeDateFormat YMD_H = new SafeDateFormat("yyyy-MM-dd HH");
	public final static SafeDateFormat YMD_HM = new SafeDateFormat("yyyy-MM-dd HH:mm");
	public final static SafeDateFormat YMD_HMS = new SafeDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SafeDateFormat YMD_HMS2 = new SafeDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
	
	public final static SafeDateFormat CYMD = new SafeDateFormat("yyyy年MM月dd日");
	public final static SafeDateFormat CYMD_H = new SafeDateFormat("yyyy年MM月dd日 HH时");
	public final static SafeDateFormat CYMD_HM = new SafeDateFormat("yyyy年MM月dd日 HH时mm分");
	public final static SafeDateFormat CYMD_HMS = new SafeDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
	
	public final static SafeDateFormat UUDT = new SafeDateFormat("yyyyMMddHHmmssSSS");
	


	
}
